package com.e3.sysmg.config.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.e3.core.base.DAOHelper;
import com.e3.core.base.ServiceException;
import com.e3.core.base.SuperQueryService;
import com.e3.sysmg.config.dao.ParameterDAO;
import com.e3.sysmg.config.entity.ParameterEntity;
import com.e3.sysmg.config.model.ParameterModel;
import com.e3.sysmg.config.model.ParameterQC;
import com.e3.sysmg.config.service.ParameterCacheManager;
import com.e3.sysmg.config.service.ParameterQueryService;

public class ParameterQueryServiceImpl extends SuperQueryService implements	ParameterQueryService {
	
	private ParameterDAO parameterDAO;
	
	private ParameterDAO getParameterDAO(){
		if(parameterDAO == null){
			parameterDAO = DAOHelper.createDAOInstance(ParameterDAO.class);
		}
		return parameterDAO;
	}

	public List<ParameterModel> getParameterByCondition(ParameterQC qc) throws ServiceException {
		
		List<Long> ids = getParameterDAO().getParameterIdsByCondition(qc);
		if(ids == null||ids.isEmpty()){
			return null;
		}
		
		List<ParameterModel> models = new ArrayList<ParameterModel>(ids.size());
		for (Long id : ids) {
			ParameterModel model = getParameterById(id);
			models.add(model);
		}
		return models;
	}
	/**
	 * the method is used to load data into cache.
	 */
	public List<ParameterModel> getAllParameters() throws ServiceException {
		List<ParameterEntity> entities = getParameterDAO().getAllParameter();
		if(entities==null||entities.isEmpty()){
			return null;
		}
		List<ParameterModel> models = new ArrayList<ParameterModel>(entities.size());
		for (ParameterEntity parameterEntity : entities) {
			models.add(parameterEntityToModel(parameterEntity));
		}
		return models;
	}

	public ParameterModel getParameterById(long id) throws ServiceException {
		ParameterModel model = ParameterCacheManager.INSTANCE.get(id);
		if(model==null){
			ParameterEntity entity = getParameterDAO().getById(ParameterEntity.class, id);
			model = parameterEntityToModel(entity);
			if(model != null){
				ParameterCacheManager.INSTANCE.put(model);
			}
		}
		
		return model;
	}

	public List<ParameterModel> getParameterByIds(List<Long> ids)	throws ServiceException {
		if(ids == null||ids.isEmpty()){
			return null;
		}
		
		List<ParameterModel> models = new ArrayList<ParameterModel>(ids.size());
		for (Long id : ids) {
			ParameterModel model = getParameterById(id);
			models.add(model);
		}
		
		return models;
	}

	private ParameterModel parameterEntityToModel(ParameterEntity entity){
		
		if(entity == null){
			return null;
		}
		
		ParameterModel model = new ParameterModel();
		model = ocEntityToModel(model, entity);
		
		model.setName(entity.getName());
		model.setType(entity.getType());
		model.setLevel(entity.getLevel());
		model.setDataType(entity.getDataType());
		model.setValue(entity.getValue());
		model.setRemark(entity.getRemark());
		
		return model;
	}

	public List<ParameterModel> getPagingParameter(ParameterQC qc, int start, int max)	throws ServiceException {
		List<Long> ids = getParameterDAO().getPagingParameterIdsByCondition(qc,start,max);
		if(ids!=null&&ids.size()>0){
			return getParameterByIds(ids);
		}
		return null;
	}

	public Long getParameterSizeByCondition(ParameterQC qc) throws ServiceException {
		return getParameterDAO().counterByCondition(qc);
	}
}
